//
//  TSoundTask.m
//  iTimeline
//
//  Created by Mobioneer HV 02 on 1/15/13.
//  Copyright (c) 2013 Mobioneer Co., Ltd. All rights reserved.
//

#import "TSoundTask.h"
@interface TSoundTask ()
{
    NSArray *listSound;
    NSMutableArray *keyList;
    NSString *soundName;
}
@property(nonatomic,weak) IBOutlet UITableView *table;
@end

@implementation TSoundTask

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    soundName = @"Default";
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(Done:)];
    self.navigationItem.rightBarButtonItem = addButton;
    self.navigationItem.title = @"Sound";

    NSString *path = [[NSBundle mainBundle] pathForResource:
                      @"ListSound" ofType:@"plist"];
    listSound = [[NSArray alloc]initWithContentsOfFile:path];
    keyList = [[NSMutableArray alloc]init];
    for(int i=0;i<[listSound count];i++)
    {
        [keyList addObject:@"0"];
    }
    self.title = @"Sound";
    if(![self.name isEqualToString:@"Default"])
    {
        for(int i=0;i<[listSound count];i++)
            if([self.name isEqualToString:[listSound objectAtIndex:i]])
            {
                [keyList replaceObjectAtIndex:i withObject:@"1"];
                break;
            }
    }
}
-(IBAction)Done:(id)sender
{
    [self.delegate returnSound:soundName];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [listSound count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [listSound objectAtIndex:indexPath.row];
    if([[keyList objectAtIndex:indexPath.row] intValue] == 1)
        cell.accessoryType = UITableViewCellStyleSubtitle ;
    else
        cell.accessoryType = UITableViewCellStyleDefault ;
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[listSound objectAtIndex:indexPath.row] ofType:@"mp3"]];
    
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    
    NSError *error;
    audioPlayer = nil;
    audioPlayer = [[AVAudioPlayer alloc]
                   initWithContentsOfURL:url
                   error:&error];
    audioPlayer.delegate = self;
    audioPlayer.volume=10.0;
    if (error)
    {
        NSLog(@"Error in audioPlayer: %@",
              [error localizedDescription]);
    } else {
        audioPlayer.delegate = self;
        [audioPlayer prepareToPlay];
        [audioPlayer play];
    }
    for(int i=0;i<[listSound count];i++)
    {
        [keyList replaceObjectAtIndex:i withObject:@"0"];
    }
    [keyList replaceObjectAtIndex:indexPath.row    withObject:@"1"];
    soundName = [listSound objectAtIndex:indexPath.row];
    [tableView reloadData];
}
@end
